﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeetCodeSolutions
{
    /// <summary>
    /// 3. 无重复字符的最长子串
    /// </summary>
    class LeetCode3: LeetCodeBase
    {
        public override void Execute()
        {
            string s = "abcabcbb";
            CommonFuntions.Print("Result :", LengthOfLongestSubstring(s).ToString());
        }

        //string s = "abcabcbb";
        public int LengthOfLongestSubstring1(string s)
        {
            if (string.IsNullOrEmpty(s)) return 0;

            List<string> subStrsList = new List<string>();
            for (int i = 0; i < s.Length; i++)
            {
                string str = "";
                for (int j = i; j < s.Length; j++)
                {
                    if (str.Contains(s[j])) break;

                    str += s[j];
                    subStrsList.Add(str);
                }
            }

            for (int i = 0; i < subStrsList.Count; i++)
            {
                CommonFuntions.Print(subStrsList[i]);
            }

            return subStrsList.Max(str => str.Length);
        }

        public int LengthOfLongestSubstring(string s)
        {
            if (string.IsNullOrEmpty(s)) return 0;

            HashSet<char> chSet = new HashSet<char>();
            int maxValue = 0;
            for (int i = 0; i < s.Length; i++)
            {
                for (int j = i; j < s.Length; j++)
                {
                    if (chSet.Contains(s[j]))
                    {
                        //chSet.Clear();
                        break;
                    }

                    chSet.Add(s[j]);
                }

                if (chSet.Count > maxValue)
                {
                    maxValue = chSet.Count;
                    CommonFuntions.Print(chSet.ToList().ToString());
                }
                chSet.Clear();
            }

            return maxValue;
        }
    }
}